<?

require_once ("application.fieldset.inc");

global $_GLOBAL_DBH;
global $_OBJECT_IFACE_PANEL;

$fs_action_request = $_REQUEST["fs_action"];

$fieldset = new Fieldset (array (
	db_conn=>$_GLOBAL_DBH,
	relation=>"users",
	fieldset=>array (
		array (name=>"id",label=>"ID",type=>"numeric",
				value=>$_REQUEST["user_id"],
				input=>"hidden",primary_key=>true),
		array (name=>"name",label=>"User Login ID",type=>"alphanumeric",
				input=>"field"),
		array (name=>"first",label=>"First",type=>"alphanumeric",
				input=>"field"),
		array (name=>"mi",label=>"MI",type=>"alphanumeric",
				input=>"field",optional=>true),
		array (name=>"last",label=>"Last",type=>"alphanumeric",
				input=>"field"),
		array (name=>"realm",label=>"User Realm",type=>"alphanumeric",
				input=>"select",
				value=>"exclusive",
				options=>array (
					array (id=>"public",name=>"Public"),
					array (id=>"private",name=>"Private"),
					array (id=>"exclusive",name=>"Exclusive"),
				),
				columns=>array(key=>"id",value=>"name"),
		),
		array (name=>"roles",label=>"Select User Roles",type=>"numeric",
				internal=>true,
				input=>"select",
				multiple=>4,
				multiple_callback=>"_app_relation_multiple",
				multiple_relation=>array (
					relation=>"user_roles",
					relation_keys=>array(
						local=>"user_id",
						foreign=>"role_id"
					),
					foreign=>"roles",
					foreign_columns=>array (key=>"id",value=>"title"),
				),
				relation=>array(foreign=>"roles",key=>"id",
						field=>"title"),
				options=>"_app_select_query",
				options_params=>"SELECT id,title FROM roles",
				columns=>array (key=>"id",value=>"title"),
		),
		array (name=>"workgroups",label=>"Select Workgroups",type=>"numeric",
				internal=>true,
				input=>"select",
				multiple=>4,
				multiple_callback=>"_app_relation_multiple",
				multiple_relation=>array (
					relation=>"user_workgroups",
					relation_keys=>array(
						local=>"user_id",
						foreign=>"workgroup_id"
					),
					foreign=>"workgroups",
					foreign_columns=>array (key=>"id",value=>"title"),
				),
				relation=>array(foreign=>"workgroups",key=>"id",
						field=>"title"),
				options=>"_app_select_query",
				options_params=>"SELECT id,title FROM workgroups",
				columns=>array (key=>"id",value=>"title"),
		),
		array (name=>"email",label=>"Email",type=>"email",
				input=>"field"),
		array (name=>"status",label=>"User Realm",type=>"alphanumeric",
				input=>"select",
				value=>"active",
				options=>array (
					array (id=>"active",name=>"Active"),
					array (id=>"inactive",name=>"Inactive"),
					array (id=>"suspended",name=>"Suspended"),
					array (id=>"closed",name=>"Closed"),
					array (id=>"hold",name=>"Hold"),
					array (id=>"new",name=>"New"),
					array (id=>"expired",name=>"Expired"),
				),
				columns=>array(key=>"id",value=>"name"),
		),
		array (name=>"timeout",label=>"Session Timeout",type=>"any",
				input=>"time",value=>_app_time ("timeout")),
		array (name=>"theme",label=>"User Theme",type=>"alphanumeric",
				input=>"select",
				value=>"contemporary",
				options=>array (
					array (id=>"contemporary",name=>"Contemporary"),
					array (id=>"default",name=>"Default Theme"),
					array (id=>"collab",name=>"System Collab"),
				),
				columns=>array(key=>"id",value=>"name"),
		),
		array (name=>"lang",label=>"Select Language",type=>"any",
				relation=>array(foreign=>"languages",key=>"id",
						field=>"name"),
				input=>"select",
				options=>"_app_select_query",
				options_params=>"SELECT id,name FROM languages",
				columns=>array (key=>"id",value=>"name"),
		),
		array (name=>"subscribed",label=>"Subscription",type=>"alphanumeric",
				input=>"select",
				value=>"true",
				options=>array (
					array (id=>"true",name=>"Subscribed"),
					array (id=>"false",name=>"Unsubscribed"),
				),
				columns=>array(key=>"id",value=>"name"),
		),
		array (name=>"image",label=>"Image File",type=>"any",
				excluded=>true,
				path=>_GLOBAL_URI_PATH . "/img/user/",
                                scale=>"174x232",
				input=>"image"),
	)
));

$fieldset->parse ();

$return = "/fs_users.php?fs_action=view&fs_tab=main&user_id={$_REQUEST["user_id"]}";

$fs_options = array (
	addnew=>array (label=>"Add New"),
	create=>array (label=>"Create"),
	modify=>array (label=>"Modify"),
	remove=>array (label=>"Remove"),
	update=>array (label=>"Update"),
	cancel=>array (label=>"Cancel"),
	delete=>array (label=>"Delete",
			redirect=>dirname ($_SERVER["PHP_SELF"]) . $return),
	canret=>array (label=>"Cancel",
			redirect=>dirname ($_SERVER["PHP_SELF"]) . $return),
	close=>array (label=>"Close",
			redirect=>dirname ($_SERVER["PHP_SELF"])),
);

$image_options = array (size=>$_REQUEST["_img_size"]);

list ($fs_input, $fs_action, $store, $valid, $success) =

	_app_process_fieldset (array (
		fieldset=>$fieldset,
		fs_options=>$fs_options,
		fs_action=>($_REQUEST["fs_action"] ?
				$_REQUEST["fs_action"] :
				"addnew"),
		fs_primary_key=>"user_id",
));

if ($store && $success) $insert_id=$fieldset->get_field ("id")->get_variable ("value");

if ($store && $success && isset ($_REQUEST["image"])) {

	if (!file_exists (_GLOBAL_ROOT_PATH . "/img/user"))

		mkdir (_GLOBAL_ROOT_PATH . "/img/user");

	_app_store_file ("image",$_SERVER["DOCUMENT_ROOT"] . _GLOBAL_URI_PATH .
			"/img/user/",
			"{$insert_id}.jpg");

}

if ($store && $success && preg_match ("/create/", $fs_action_request)) {
	print ("INSERT INTO profiles (uid,country,state) VALUES ({$insert_id},223,6)");
	$_GLOBAL_DBH->query ("INSERT INTO profiles (uid,country,state) VALUES ({$insert_id},223,6)");

}

$_OBJECT_FIELDSET_CONTROL =_app_create_fieldset_object (array (
	fieldset_id=>"fs_user_users",
	db_conn=>$_GLOBAL_DBH,
	fieldset=>$fieldset,
	fs_enctype=>"multipart/form-data",
	fs_action=>$fs_action,
	fs_input=>$fs_input,
	fs_link=>"fs_users.php?fs_action=view&fs_tab=main&user_id={$_REQUEST["user_id"]}",
	fs_options=>$fs_options,
));

$_OBJECT_IFACE_TABBING_PANEL->insert_container ($_OBJECT_FIELDSET_CONTROL);

?>
